package com.cxc.news.interceptor;

import com.cxc.news.pojo.Type;
import com.cxc.news.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Component
public class TypeInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    TypeService typeService;

    /**
     * 确保全局都有分类列表
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        List<Type> types = (List<Type>) request.getSession().getAttribute("types");
        if (types!=null){
            return true;
        }
        request.getSession().setAttribute("types",typeService.getTypeList());

        System.out.println((List<Type>) request.getSession().getAttribute("types"));
        return true;
    }
}
